package com.sky.controller.user;

import com.sky.constant.StatusConstant;
import com.sky.entity.Dish;
import com.sky.result.Result;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import io.netty.util.concurrent.SucceededFuture;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController("userDishController")
@RequestMapping("/user/dish")
@Slf4j
@Api(tags = "C端-菜品浏览相关接口")
public class DishController {
    @Autowired
    private DishService dishService;
    @Autowired
    private RedisTemplate redisTemplate;

    @GetMapping("/list")
    public Result<List<DishVO>> list(Long categoryId){
        //查询redis当中是否存在该套餐信息
        String key = "dish_"+ categoryId;
        //如果存在直接返回
        //存进去的时候是什么类型取出来就是什么类型
        List<DishVO> dishVOList = (List<DishVO>) redisTemplate.opsForValue().get(key);
        if(dishVOList !=null && dishVOList.size()>0){
            return Result.success(dishVOList);
        }
        //如果不存在 从数据库中查询 并存入redis
        log.info("根据分类id查询菜品:{}",categoryId);
        Dish dish = new Dish();
        dish.setCategoryId(categoryId);
        dish.setStatus(StatusConstant.ENABLE);//查询启售中的菜品

        List<DishVO> dishVOs = dishService.listWithFlavor(dish);
        redisTemplate.opsForValue().set(key,dishVOs);//存入redis中
        return Result.success(dishVOs);
    }
}
